define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    "index_url": "goods/cate/index",
                    "add_url": "goods/cate/add",
                    "edit_url": "goods/cate/edit",
                    "del_url": "goods/cate/del",
                    "multi_url": "goods/cate/multi",
                    "table": "auth_rule"
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'weigh',
                escape: false,
                columns: [
                    [
                        {field: 'state', checkbox: true,},
                        {field: 'id', title: 'ID',formatter:function(value,row){
                                return '<div class="cid" style="text-align: center;" data-auto="'+row.is_auto+'">'+value+'</div>'
                            }},
                        // {field: 'name', title: __('Name'), align: 'left'},
                        // {field: 'icon', title: __('Icon'), formatter: Controller.api.formatter.icon},
                        {field: 'name', title: __('Name'), align: 'left', formatter: Controller.api.formatter.name},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status},
                        // {
                        //     field: 'ismenu',
                        //     title: __('Ismenu'),
                        //     align: 'center',
                        //     formatter: Table.api.formatter.toggle
                        // },
                        {
                            field: 'id',
                            title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle"><i class="fa fa-chevron-up"></i></a>',
                            operate: false,
                            formatter: Controller.api.formatter.subnode
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                        //         var that = $.extend({}, this);
                        //         var table = $(that.table).clone(true)
                        //         // $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                        //         // console.log(row)
                        //         if(row['ystatus'] != 1){
                        //
                        //             $(table).data("operate-edit", null);// 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                        //             $(table).data("operate-del", null);// 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                        //         }
                        //         that.table = table;
                        //         return Table.api.formatter.operate.call(that, value, row, index);
                        //     }}
                    ]
                ],
                pagination: false,
                search: false,
                commonSearch: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $('.btn-auto').click(function () {
                layer.confirm('确定要同步商城分类？<br/>同步之后会重新覆盖分类内容', {
                    icon: 3,
                    title:'提示'
                }, function(index){
                    index2 = layer.load()
                    url = Config.this_url+'/goods/cate/auto';
                    $.post(url,{},function (res) {
                        layer.msg(res.msg)
                        layer.close(index);
                        layer.close(index2);
                        if(res.code == 1){
                            setTimeout(function () {
                                $('.btn-refresh').click()
                            },2000)

                        }
                    })
                });

            })
            //当内容渲染完成后
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $('tbody').find('div').each(function () {
                    if($(this).data('auto') == 1){
                        $(this).parents('tr').find('.btn-delone').hide()
                    }
                })


                //默认隐藏所有子节点
                $("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").hide();
                $(".btn-node-sub.disabled").closest("tr").hide();

                //显示隐藏子节点
                $(".btn-node-sub").off("click").on("click", function (e) {
                    var status = $(this).data("shown") ? true : false;
                    $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
                        $(this).closest("tr").toggle(!status);
                    });
                    $(this).data("shown", !status);
                    return false;
                });
                //点击切换/排序/删除操作后刷新左侧菜单
                $(".btn-change[data-id],.btn-delone,.btn-dragsort").data("success", function (data, ret) {
                    Fast.api.refreshmenu();
                    return false;
                });

            });
            //批量删除后的回调
            $(".toolbar > .btn-del,.toolbar .btn-more~ul>li>a").data("success", function (e) {
                Fast.api.refreshmenu();
            });
            //展开隐藏一级
            $(document.body).on("click", ".btn-toggle", function (e) {
                $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
                var that = this;
                var show = $("i", that).hasClass("fa-chevron-down");
                $("i", that).toggleClass("fa-chevron-down", !show);
                $("i", that).toggleClass("fa-chevron-up", show);
                $("a.btn[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
                $(".btn-node-sub[data-pid=0]").data("shown", show);
            });
            //展开隐藏全部
            $(document.body).on("click", ".btn-toggle-all", function (e) {
                var that = this;
                var show = $("i", that).hasClass("fa-plus");
                $("i", that).toggleClass("fa-plus", !show);
                $("i", that).toggleClass("fa-minus", show);
                $(".btn-node-sub.disabled").closest("tr").toggle(show);
                $(".btn-node-sub").data("shown", show);
            });
        },
        // index: function () {
        //     // 初始化表格参数配置
        //     Table.api.init({
        //         extend: {
        //             index_url: 'goods/cate/index',
        //             add_url: 'goods/cate/add',
        //             edit_url: 'goods/cate/edit',
        //             dragsort_url: '',
        //         }
        //     });
        //
        //     var table = $("#table");
        //     var tableOptions = {
        //         url: $.fn.bootstrapTable.defaults.extend.index_url,
        //         escape: false,
        //         pk: 'id',
        //         sortName: 'weigh',
        //         pagination: false,
        //         commonSearch: false,
        //         search: false,
        //         columns: [
        //             [
        //                 {checkbox: true},
        //                 {field: 'id', title: __('Id')},
        //                 {field: 'name', title: __('Name'),align: 'left',width:'200'},
        //                 {field: 'weigh', title: __('Weigh')},
        //                 {field: 'status', title: __('Status'), operate: false, formatter: Table.api.formatter.status},
        //                 {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
        //             ]
        //         ]
        //     };
        //     // 初始化表格
        //     table.bootstrapTable(tableOptions);
        //
        //     // 为表格绑定事件
        //     Table.api.bindevent(table);
        //
        //     //绑定TAB事件
        //     $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
        //         // var options = table.bootstrapTable(tableOptions);
        //         var typeStr = $(this).attr("href").replace('#', '');
        //         var options = table.bootstrapTable('getOptions');
        //         options.pageNumber = 1;
        //         options.queryParams = function (params) {
        //             // params.filter = JSON.stringify({type: typeStr});
        //             params.type = typeStr;
        //
        //             return params;
        //         };
        //         table.bootstrapTable('refresh', {});
        //         return false;
        //
        //     });
        //
        //     //必须默认触发shown.bs.tab事件
        //     // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        //
        // },
        add: function () {
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                title: function (value, row, index) {
                    return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
                },
                name: function (value, row, index) {
                    return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
                },
                icon: function (value, row, index) {
                    return '<span class="' + (!row.ismenu || row.status == 'hidden' ? 'text-muted' : '') + '"><i class="' + value + '"></i></span>';
                },
                subnode: function (value, row, index) {
                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '
                        + (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';
                }
            },
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});